﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace SolutionFactory
{
    public class CommonFunction
    {
        #region 公用方法
        /// <summary>
        /// sql数据类型转换成c#数据类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string SqlToCSharpType(string type)
        {
            string result = string.Empty;
            switch (type.ToLower())
            {
                case "int":
                    result = "Int32";
                    break;
                case "text":
                    result = "String";
                    break;
                case "bigint":
                    result = "Int64";
                    break;
                case "binary":
                    result = "Byte[]";
                    break;
                case "bit":
                    result = "Boolean";
                    break;
                case "char":
                    result = "String";
                    break;
                case "datetime":
                    result = "DateTime";
                    break;
                case "decimal":
                    result = "Decimal";
                    break;
                case "float":
                    result = "Double";
                    break;
                case "image":
                    result = "Byte[]";
                    break;
                case "money":
                    result = "Decimal";
                    break;
                ///////
                case "nchar":
                    result = "String";
                    break;
                case "ntext":
                    result = "String";
                    break;
                case "numeric":
                    result = "Decimal";
                    break;
                case "nvarchar":
                    result = "String";
                    break;
                case "real":
                    result = "Single";
                    break;
                case "smalldatetime":
                    result = "DateTime";
                    break;
                case "smallint":
                    result = "Int16";
                    break;
                case "smallmoney":
                    result = "Decimal";
                    break;
                case "timestamp":
                    result = "DateTime";
                    break;
                case "tinyint":
                    result = "Byte";
                    break;
                case "uniqueidentifier":
                    result = "Guid";
                    break;
                case "varbinary":
                    result = "Byte[]";
                    break;
                case "varchar":
                    result = "String";
                    break;
                case "Variant":
                    result = "Object";
                    break;
                ///////
                default:
                    result = "String";
                    break;
            }
            return result;
        }
        /// <summary>
        /// 首字母大写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FirstNumberToUpper(string str)
        {
            str = str.Substring(0, 1).ToUpper() + str.Substring(1);
            return str;
        }
        /// <summary>
        /// 根据表名获取表的描述信息
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string GetTableDescriptByName(string tableName)
        {
            string sql = string.Format(@"SELECT  [value]
                    FROM  ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table','{0}',  NULL, NULL)", tableName);
            object value = DBHelper.GetScalar(sql);
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 根据表名和列名获取列的描述信息
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static string GetFieldDescriptByTableNameAndColumnName(string tableName, string columnName)
        {
            string sql = string.Format(@"SELECT  [value]
                    FROM  ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table','{0}', 'column', '{1}')", tableName, columnName);
            object value = DBHelper.GetScalar(sql);
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
            
        /// <summary>
        /// 生成解决方案文件
        /// </summary>
        public static void GenerateSlnFile()
        {
            WriteFiles.WriteSolutionFile();
        }
        /// <summary>
        /// 工程文件
        /// </summary>
        public static void GenerateProjectFile(LibraryClassInfo libInfo)
        {
            WriteFiles.WriteCsprojFile(libInfo);
        }
        /// <summary>
        /// 程序集信息
        /// </summary>
        public static void GenerateAssemblyInfo(LibraryClassInfo libInfo)
        {
            WriteFiles.WriteAssemblyInfo(libInfo.AssemblyFloderPath, libInfo.Guid);
        }
        #endregion

        #region 生成实体层
        /// <summary>
        /// 根据表名生成实体类
        /// </summary>
        /// <param name="tables"></param>
        public static void GenerateEntityClasses()
        {
            foreach (var item in GlobalProperty.entitysInfo)
            {
                string sql = string.Format(@"SELECT
	                                        c.name AS columname,
	                                        t.name AS typename
                                        FROM
	                                        syscolumns c
                                        INNER JOIN systypes t ON c.xtype = t.xtype
                                        WHERE
	                                        id = object_id('{0}')", item.TableName);
                DataTable table = DBHelper.ReadTable(sql);
                foreach (DataRow row in table.Rows)
                {
                    EntityFieldsInfo field = new EntityFieldsInfo();
                    field.FieldName = row["columname"].ToString();
                    field.TypeName = SqlToCSharpType(row["typename"].ToString());
                    field.TableColumnName = row["columname"].ToString();
                    field.Description = GetFieldDescriptByTableNameAndColumnName(item.TableName, field.TableColumnName);
                    item.Fields.Add(field);
                }
                //写入文件
                WriteFiles.WriteEntityFiles();
            }
        }
        #endregion

        #region 生成实体工厂层

        public static void GenerateEntityFactory()
        {
            WriteFiles.WriteBuildEntity();
        }
            
        #endregion

        #region 生成数据库操作
        public static void GenerateDBAccess()
        {
            WriteFiles.WriteDBAccess();
        }
        #endregion
    }
}
